import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic

Rectangle {
    id: importWords

    anchors.fill: parent
    color: "black"
    opacity: 0.8

    property int cntWord: 0
    property bool isOk: false

    Item {
        id: wrapperImg
        height: 140
        width: 140
        anchors.bottom: textWait.top
        anchors.bottomMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter
        Image {
            id: imgImport
            anchors.centerIn: parent
            source: "images/import.png"
        }
    }

    Text {
        id: textWait
        property int progresValue: 0
        anchors.centerIn: parent
        font.pixelSize: 30
        color: "white"
        text: qsTr("Please wait...")
    }
    ProgressBar {
        id: pbImport
        indeterminate: true
        anchors.top: textWait.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        width: 0.7*parent.width
        visible: ma.enabled === false ? true : false
    }

    MouseArea {
        id: ma
        anchors.fill: parent
        enabled: true
        onClicked: {
            importWords.visible = false
        }
    }

    property int importCategoryId: pageImportWords.selectedCategoryId

    /************************************************************************/
    /************************************************************************/

    XmlListModel {
        id: xmlWords
        source: pageImportWords.xmlfile
        query: "/meewords/words"
        XmlRole { name: "wrd"; query: "word/string()" }
        XmlRole { name: "trans"; query: "translation/string()" }
    }

    Repeater {
        id: repImport
        model: xmlWords
        delegate: Item {
            Component.onCompleted: {
                if( (pageImportWords.selectedCategoryId > 0) && (pageImportWords.xmlfile !== "") ) {
                    //console.log( "*" + pageImportWords.xmlfile)
                    ma.enabled = false
                    if( (wrd !== "") && (trans !== "") )
                        var id = Logic.addWord( wrd, trans, pageImportWords.selectedCategoryId )
                    cntWord += 1
                    textWait.progresValue += 1
                    if ( cntWord == repImport.count ) {
                        importWords.visible = false
                        ma.enabled = true
                        if( cntWord > 0) {
                            showDlgImport() //показываем диалог окончания импорта
                            cntWord = -10 //сбрасываем счетчик
                        }
                    }
                    //console.log("word: id="+id)
                    //console.log("textWait.progresValue="+textWait.progresValue)
                } else if( pageImportWords.xmlfile === "" ) {
                    textWait.text = qsTr("Please, selected xml file for import")
                    ma.enabled = true
                } else if( pageImportWords.selectedCategoryId <= 0 ) {
                    textWait.text = qsTr("Please, selected category")
                    ma.enabled = true
                } else {
                    textWait.text = qsTr("Sorry, internal error")
                    ma.enabled = true
                }
            }
        }
    } //Repeater

    QueryDialog {
        id: dlgExport
        titleText: qsTr("Import")
        icon: "images/import.png"
        message: qsTr( "Import completed successfully" )
        onRejected: {
            pageStack.pop()
        }
    }
    function showDlgImport() {
        dlgExport.open()
        //_txtTF.text = ""
    }
}
